#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

class Solution{
    public:
    //可读性差
    int ans1(vector<int>& num){
        sort(num.begin(), num.end());
        int h = 0, i = num.size() - 1;
        while (i >= 0 && num[i] > h) {
            h++;
            i--;
        }
        return h;       
    }

    //计数排序
    int ans2(vector<int>& citations) {
        int n = citations.size(), tot = 0;
        vector<int> counter(n + 1);
        for (int i = 0; i < n; i++) {
            if (citations[i] >= n) {
                counter[n]++;
            } else {
                counter[citations[i]]++;
            }
        }
        for (int i = n; i >= 0; i--) {
            tot += counter[i];
            if (tot >= i) {
                return i;
            }
        }
        return 0;
    }

    //二分法
    int ans3(vector<int>& citations) {
        int n = citations.size(), tot = 0;
        vector<int> counter(n + 1);
        for (int i = 0; i < n; i++) {
            if (citations[i] >= n) {
                counter[n]++;
            } else {
                counter[citations[i]]++;
            }
        }
        for (int i = n; i >= 0; i--) {
            tot += counter[i];
            if (tot >= i) {
                return i;
            }
        }
        return 0;
    }
    
    //error
    int myans(vector<int>& num){
        int length = num.size();
        int count;
        int thr = 0;
        if(length == 1){
            if(num[0] == 0){
                return 0;
            }else{
                return 1;
            }
        }
        while(true){
            count = 0;
            for(int i = 0; i != length; i++){
                if(num[i] >= thr){
                    count++;
                }
            }
            if(count > thr){
                thr++;
            }
            if(thr == count){
                break;
            }
            if(thr > count){
                thr--;
                break;
            }
        }
        return thr;
    }

};

void test1(){
    vector<int> num = {3, 0, 6, 1, 5};

    Solution s;
    // int ret = s.ans(num);
    int ret = s.myans(num);
    cout << ret << endl;
}

void test2(){
    vector<int> num = {0};

    Solution s;
    // int ret = s.ans(num);
    int ret = s.myans(num);
    cout << ret << endl;
}

void test3(){
    vector<int> num = {0, 4, 4, 5, 6};

    Solution s;
    // int ret = s.ans(num);
    int ret = s.myans(num);
    cout << ret << endl;
}

void test4(){
    vector<int> num = {0, 0, 0};

    Solution s;
    // int ret = s.ans(num);
    int ret = s.myans(num);
    cout << ret << endl;
}

void test5(){
    vector<int> num = {1, 1};

    Solution s;
    // int ret = s.ans(num);
    int ret = s.myans(num);
    cout << ret << endl;
}

int main(void){
    test1();
    test2();
    test3();
    test4();
    test5();

    return 0;
}